import Vue from 'vue'
import VueRouter from 'vue-router'
//全局引入
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      showTabBar: true
    },
    children: [{
      path: "index",
      name: "Index",
      component: () => import('../views/Home/children/Index'),
      meta: {
        showTabBar: true
      }
    }, {
      path: "other",
      name: "Other",
      component: () => import('../views/Home/children/Other'),
      meta: {
        showTabBar: true
      }
    }, {
      path: "/home",
      redirect: "/home/index"
    }]
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('../views/Category/Category.vue'),
    meta: {
      showTabBar: true
    }
  },
  {
    path: '/find',
    name: 'Find',
    component: () => import('../views/Find/Find.vue'),
    meta: {
      showTabBar: true
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/Cart/Cart.vue'),
    meta: {
      showTabBar: true
    }
  },
  {
    path: '/mine',
    name: 'Mine',
    component: () => import('../views/Mine/Mine.vue'),
    meta: {
      showTabBar: true
    }
  },
  {
    path: '/userlogin',
    name: 'UserLogin',
    component: () => import('../views/Login/UserLogin.vue')
  },
  {
    path: '/phonelogin',
    name: 'PhoneLogin',
    component: () => import('../views/Login/PhoneLogin.vue')  //按需引入
  },
  {
    path: '/categorylist/:cat_id',
    name: 'CategoryList',
    component: () => import('../views/Category/components/CategoryList.vue')  //按需引入
  }, {
    path: "/",
    redirect: "/userlogin"
  }
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  //to 即将进入的目标
  //from 即将离开的路由
  //next 放行
  var token = localStorage.getItem("token")
  // console.log(token);
  // console.log(to);
  // console.log(from);
  if (to.path == "/userlogin" || to.path == "/phonelogin") {
    next()
    return
  }
  if (!token) return next("/userlogin")  //如果token不存在，直接跳转到登录页面

  next()
})

export default router
